#include <boost/asio.hpp>
#include <iostream>
#include <string>

using boost::asio::ip::tcp;

void send_message(tcp::socket &socket, const std::string &message)
{
    boost::asio::write(socket, boost::asio::buffer(message));
}

std::string read_message(tcp::socket &socket, std::size_t length)
{
    std::vector<char> reply(length);
    size_t reply_length = boost::asio::read(socket, boost::asio::buffer(reply, length));
    return std::string(reply.data(), reply_length);
}

int main()
{
    try
    {
        for (;;)
        {
            boost::asio::io_context io_context;
            tcp::resolver resolver(io_context);
            auto endpoints = resolver.resolve("127.0.0.1", "12345");
            tcp::socket socket(io_context);
            boost::asio::connect(socket, endpoints);

            std::cout << "Enter message: ";
            std::string message;
            std::getline(std::cin, message);

            send_message(socket, message);

            std::string reply = read_message(socket, message.size());
            std::cout << "Reply: " << reply << "\n";
        }
    }
    catch (std::exception &e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }

    return 0;
}
